<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Comment extends CI_Controller {

    // num of records per page
    private $limit = 10;

    function __construct() {
        parent::__construct();

        // load library
        $this->load->library(array('table', 'form_validation'));

        // load helper
        $this->load->helper('url');

        // load model
        $this->load->model('Comment_model', '', TRUE);
    }

    function index($offset = 0) {
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);

        // load data
        $comments = $this->Comment_model->get_paged_list($this->limit, $offset)->result();

        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('comment/index/');
        $config['total_rows'] = $this->Comment_model->count_all();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        // generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('No', 'Name', 'Gender', 'Date of Birth (dd-mm-yyyy)', 'Actions');
        $i = 0 + $offset;
        foreach ($comments as $comment) {
            $this->table->add_row(++$i, $comment->name, strtoupper($comment->gender) == 'M' ? 'Male' : 'Female', date('d-m-Y', strtotime($comment->dob)), anchor('comment/view/' . $comment->id, 'view', array('class' => 'view')) . ' ' .
                    anchor('comment/update/' . $comment->id, 'update', array('class' => 'update')) . ' ' .
                    anchor('comment/delete/' . $comment->id, 'delete', array('class' => 'delete', 'onclick' => "return confirm('Are you sure want to delete this comment?')"))
            );
        }
        $data['table'] = $this->table->generate();

        // load view
        $this->load->view('commentList', $data);
    }

    function add() {
        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // set common properties
        $data['title'] = 'Add new comment';
        $data['message'] = '';
        $data['action'] = site_url('comment/addcomment');
        $data['link_back'] = anchor('comment/index/', 'Back to list of comments', array('class' => 'back'));

        // load view
        $this->load->view('commentEdit', $data);
    }

    function addcomment() {
        // set common properties
        $data['title'] = 'Add new comment';
        $data['action'] = site_url('comment/addcomment');
        $data['link_back'] = anchor('comment/index/', 'Back to list of comments', array('class' => 'back'));

        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // run validation
        if ($this->form_validation->run() == FALSE) {
            $data['message'] = '';
        } else {
            // save data
            $comment = array('name' => $this->input->post('name'),
                'gender' => $this->input->post('gender'),
                'dob' => date('Y-m-d', strtotime($this->input->post('dob'))));
            $id = $this->Comment_model->save($comment);

            // set user message
            $data['message'] = '<div class="success">add new comment success</div>';
        }

        // load view
        $this->load->view('commentEdit', $data);
    }

    function view($id) {
        // set common properties
        $data['title'] = 'comment Details';
        $data['link_back'] = anchor('comment/index/', 'Back to list of comments', array('class' => 'back'));

        // get comment details
        $data['comment'] = $this->Comment_model->get_by_id($id)->row();

        // load view
        $this->load->view('commentView', $data);
    }

    function update($id) {
        // set validation properties
        $this->_set_rules();

        // prefill form values
        $comment = $this->Comment_model->get_by_id($id)->row();
        $this->form_data->id = $id;
        $this->form_data->name = $comment->name;
        $this->form_data->gender = strtoupper($comment->gender);
        $this->form_data->dob = date('d-m-Y', strtotime($comment->dob));

        // set common properties
        $data['title'] = 'Update comment';
        $data['message'] = '';
        $data['action'] = site_url('comment/updatecomment');
        $data['link_back'] = anchor('comment/index/', 'Back to list of comments', array('class' => 'back'));

        // load view
        $this->load->view('commentEdit', $data);
    }

    function updatecomment() {
        // set common properties
        $data['title'] = 'Update comment';
        $data['action'] = site_url('comment/updatecomment');
        $data['link_back'] = anchor('comment/index/', 'Back to list of comments', array('class' => 'back'));

        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // run validation
        if ($this->form_validation->run() == FALSE) {
            $data['message'] = '';
        } else {
            // save data
            $id = $this->input->post('id');
            $comment = array('name' => $this->input->post('name'),
                'gender' => $this->input->post('gender'),
                'dob' => date('Y-m-d', strtotime($this->input->post('dob'))));
            $this->Comment_model->update($id, $comment);

            // set user message
            $data['message'] = '<div class="success">update comment success</div>';
        }

        // load view
        $this->load->view('commentEdit', $data);
    }

    function delete($id) {
        // delete comment
        $this->Comment_model->delete($id);

        // redirect to comment list page
        redirect('comment/index/', 'refresh');
    }

    // set empty default form field values
    function _set_fields() {
        $this->form_data->id = '';
        $this->form_data->name = '';
        $this->form_data->gender = '';
        $this->form_data->dob = '';
    }

    // validation rules
    function _set_rules() {
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('gender', 'Gender', 'trim|required');
        $this->form_validation->set_rules('dob', 'DoB', 'trim|required|callback_valid_date');

        $this->form_validation->set_message('required', '* required');
        $this->form_validation->set_message('isset', '* required');
        $this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
    }

    // date_validation callback
    function valid_date($str) {
        //match the format of the date
        if (preg_match("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts)) {
            //check weather the date is valid of not
            if (checkdate($parts[2], $parts[1], $parts[3]))
                return true;
            else
                return false;
        }
        else
            return false;
    }

}

?>